#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct student
{
	int number;
	char name[8];
	int num_sex,num_code;
	int score[10],sum,ave;
	char sex[3], code[12];
}st[100],*p=st, temp;
struct codein
{
	int nn;
	char xy[12];
}co[10],*q=co;
void swap_code(struct student st[], int num1,struct codein co[],int num2）//将学院与输入数字对应起来
{
	int i,j;
	for (i = 0; i < num1; i++)
	{
		for (j = 0; j < num2; j++)
		{
			if (st[i].num_code == co[j].nn)strcpy(st[i].code, co[j].xy);
		}
	}
}


void avescore(struct student st[], int num)
{
	int i;
	for (i = 0; i < num; i++)
	{
		st[i].sum = st[i].score[0] + st[i].score[1] + st[i].score[2] + st[i].score[3] + st[i].score[4] + st[i].score[5] + st[i].score[6] + st[i].score[7] + st[i].score[8] + st[i].score[9];
		st[i].ave = st[i].sum / 10;
	}
}


void output(int i)//打印列表
{
	printf("%-8d", st[i].number);
	printf("%-8s", st[i].name);
	printf("%-12s", st[i].code);
	printf("%-5d", st[i].score[0]);
	printf("%-5d", st[i].score[1]);
	printf("%-5d", st[i].score[2]);
	printf("%-5d", st[i].score[3]);
	printf("%-5d", st[i].score[4]);
	printf("%-5d", st[i].score[5]);
	printf("%-5d", st[i].score[6]);
	printf("%-5d", st[i].score[7]);
	printf("%-5d", st[i].score[8]);
	printf("%-5d", st[i].score[9]);
	printf("%-5d\n", st[i].ave);
}

void downsort(struct student st[], int num)//平均成绩降序排序
{
	int i, j;
	for (i = 0; i < num - 1; i++)
	{
		for (j = i + 1; j < num; j++)
		{
			if (st[i].ave < st[j].ave)
			{
				temp = st[i];
				st[i] = st[j];
				st[j] = temp;
			}
		}
	}

	printf("学号    姓名    学院        s1   s2   s3   s4   s5   s6   s7   s8   s9   s10  ave\n");
	for (i = 0; i < num;i++)output(i);
}

void upsort(struct student st[], int num)//姓名升序排序
{
	int i, j;
	for (i = 0; i < num - 1; i++)
	{
		for (j = i + 1; j < num; j++)
		{
			if (strcmp(st[i].name,st[j].name)>0)
			{
				temp = st[i];
				st[i] = st[j];
				st[j] = temp;
			}
		}
	}

	printf("学号    姓名    学院        s1   s2   s3   s4   s5   s6   s7   s8   s9   s10  ave\n");
	for (i = 0; i < num; i++)output(i);
}

void searchxh(struct student st[], int num)//学号查询
{
	int a,i;
	while (1)
	{
		printf("请输入学号(输入 -1 停止查询)：");
		scanf("%d", &a);
		if (a == -1)break;
		for (i = 0; i < num; i++)
		{
			if (a == st[i].number)output(i);
		}
	}
}

void searchxm(struct student st[], int num)//成绩查询
{
	char n[8],m[]="x";
	int i;
	while (1)
	{
		printf("请输入姓名(输入 x 停止查询)：");
		scanf("%s", n);
		if (strcmp(n,m)==0)break;
		for (i = 0; i < num; i++)
		{
			if (strcmp(n,st[i].name)==0)output(i);
		}
	}
}

void listcode(struct student st[], int num)//学院学生列表
{
	int a,i;
	printf("请输入学院代号：");
	scanf("%d",&a);
	for (i = 0; i < num; i++)
	{
		if (a == st[i].num_code)output(i);
	}
}

void tongji(struct student st[], int num)//平均成绩统计
{
	int A, B, C, D, E,i;

	A = 0, B = 0, C = 0, D = 0, E = 0;
	for (i = 0; i<num; i++)
	{
		if(st[i].ave<60)E++;
		if(st[i].ave>=60&&st[i].ave<=69)D++;
		if(st[i].ave>=70&&st[i].ave<=79)C++;
		if(st[i].ave>=80&&st[i].ave<=89)B++;
		if(st[i].ave>=90)A++;
	}
	printf("分数段      学生人数     图形\n");
	printf("E:<60       %-2d(%-2.0f%%)      ", E, (float)E / num * 100);
	for (i = 0; i<E; i++)printf("*");
	printf("\n");

	printf("D:60-69     %-2d(%-2.0f%%)      ", D, (float)D / num * 100);
	for (i = 0; i<D; i++)printf("*");
	printf("\n");

	printf("C:70-79     %-2d(%-2.0f%%)      ", C, (float)C / num * 100);
	for (i = 0; i<C; i++)printf("*");
	printf("\n");

	printf("B:80-89     %-2d(%-2.0f%%)      ", B, (float)B / num * 100);
	for (i = 0; i<B; i++)printf("*");
	printf("\n");

	printf("A:>=90      %-2d(%-2.0f%%)      ", A, (float)A / num * 100);
	for (i = 0; i<A; i++)printf("*");
	printf("\n");
}
int main()
{

	FILE *fp1,*fp2;
	int num=100,num2=10,i,choose;

	if ((fp1 = fopen("D:\\C语言\\实践\\题目4\\ConsoleApplication1\\file\\StudentInfo.txt", "rt")) == NULL)//文件指针名=fopen（文件名，使用文件方式）,文件指针名必须被说明为FILE类型的指针变量。
	{
		printf("Cannot open file");
		exit(1);
	}
	else printf("input StudentInfo.txt\n");

	for (i = 0; i < num; i++, p++)
	{
		fscanf(fp1, "%d %s %d %d %d %d %d %d %d %d %d %d %d %d", &p->number, p->name, &p->num_sex, &p->num_code, &p->score[0], &p->score[1], &p->score[2], &p->score[3], &p->score[4], &p->score[5], &p->score[6], &p->score[7], &p->score[8], &p->score[9]);
	}

	fclose(fp1);

	if ((fp2 = fopen("D:\\C语言\\实践\\题目4\\ConsoleApplication1\\file\\CodeInfo.txt", "rt")) == NULL)
	{
		printf("Cannot open file");
		exit(1);
	}
	else printf("input CodeInfo.txt\n");

	for (i = 0; i < 10;i++,q++)
	{
		fscanf(fp2, "%d %s", &q->nn, q->xy);
	}

	fclose(fp2);

	swap_code( st, num, co, num2);

	avescore(st, num);

	while (1)
	{
		printf("成绩统计与分析\n");
		printf("1.平均成绩降序排序\n");
		printf("2.名字升序排序\n");
		printf("3.学号查询\n");
		printf("4.名字查询\n");
		printf("5.学院学生列表\n");
		printf("6.平均成绩统计\n");
		printf("7.退出\n");

		printf("请输入序号：");
		scanf("%d", &choose);

		if (choose == 1)
		{
			downsort(st, num);
			break;
		}
		if (choose == 2)
		{
			upsort(st, num);
			break;
		}
		if (choose == 3)searchxh(st, num);
		if (choose == 4)searchxm(st, num);
		if (choose == 5)
		{
			listcode(st, num);
			break;
		}
		if (choose == 6)
		{
			tongji(st, num);
			break;
		}
		if (choose == 7)break;
	}
	
	return 0;
}